/*
 * Graph.cpp
 *
 *  Created on: 28/08/2014
 *      Author: santosma
 */

#include "Graph.h"

Graph::Graph(int n){
	this->n = n;
	m = 0;
	aux = new BitVector(n);
	edges = new BitVector*[n];
	for(int i = 0; i < n; i++) edges[i] = new BitVector(n);
}

Graph::Graph(FILE* file){
	int u, v, ret;
	char space;
	ret = fscanf(file, "%d",&n);//n
	m = 0;
	aux = new BitVector(n);
	edges = new BitVector*[n];
	for(int i = 0; i < n; i++) edges[i] = new BitVector(n);

	while(!feof(file)){
		ret = fscanf(file, "%d",&u);
		ret = fscanf(file, "%c",&space);
		ret = fscanf(file, "%d",&v);
		if(ret == 0) {
			break;
			printf("PROBLEMAS NA LEITURA DO GRAFO!!!\n");
		}
		addEdge(u-1,v-1);
	}
}

Graph::~Graph(){
	for(int i = 0; i < n; i++)delete edges[i];
	delete edges;
}

long Graph::numV(){
	return n;
}

long Graph::numE(){
	return m;
}

void Graph::addEdge(int u, int v){
	edges[u]->set(v);
	edges[v]->set(u);
	m++;
}

void Graph::removeEdge(int u, int v){
	edges[u]->unset(v);
	edges[v]->unset(u);
	m--;
}

bool Graph::hasEdge(int u, int v){
	return edges[u]->isSet(v);
}

int Graph::degree(int v){
	return edges[v]->count();
}

int Graph::degreeOn(int v, BitVector* nV){
	aux->setAll();
	aux->dot(nV);
	aux->dot(edges[v]);
	return aux->count();
}

BitVector* Graph::neig(int v){
	return edges[v]->copy();
}

void Graph::print(){
	printf("\nGRAPH!\n");
	printf("NUMERO DE VERTICES = %d NUMERO DE ARESTAS = %d \n", n, m);
	for(int i = 0; i < n; i++){
		for(int j = i+1; j < n; j++){
			if(hasEdge(i,j)) printf("-(%d, %d)-", i, j);
		}
	}
}
